package com.za.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.github.pagehelper.PageInterceptor;
import com.za.framework.filter.SqlLogPrintInterceptor;
import com.za.permission.interceptor.SqlRewriteInterceptor;
import com.za.permission.interceptor.SqlVagueQueryUserInfoInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author WangYan
 * @desc: 手动加载 拦截器，方便控制代码执行顺序
 * @date 2022/11/18 19:02
 */
@Configuration
public class MybatisConfiguration {

    @Resource
    private List<SqlSessionFactory> sqlSessionFactoryList;

    /**
     * 最后加载拦截器，最先执行
     *
     * @Author WangYan
     * @Date 2022/11/21 15:18
     */
    @PostConstruct
    public void envInterceptor() {
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(new PageInterceptor());
            sqlSessionFactory.getConfiguration().addInterceptor(paginationInterceptor());
            sqlSessionFactory.getConfiguration().addInterceptor(new SqlLogPrintInterceptor());
            sqlSessionFactory.getConfiguration().addInterceptor(new SqlRewriteInterceptor());
            sqlSessionFactory.getConfiguration().addInterceptor(new SqlVagueQueryUserInfoInterceptor());
        }
    }

    private PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        paginationInterceptor.setLimit(10000);
//        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}
